﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Maticsoft.Model;

namespace CallCenter.DAL
{
    partial class CustomersDAL
    {
        public CustomersModel ToCustomer(DataRow row)
        {
            CustomersModel customer = new CustomersModel();
            customer.Id = (Guid?)row["Id"];
            customer.Name =(string) row["Name"];
            customer.NamePinyin = (string)row["NamePinyin"];
            customer.TelPhone = (string)row["TelPhone"];
            customer.MobilePhone = (string)row["MobilePhone"];
            customer.PostCode = (string)row["PostCode"];
            customer.Email = (string)row["Email"];
            customer.Address = (string)row["Address"];
            customer.DeptNo = (string)row["DeptNo"];
            customer.BuyDate = (DateTime?)row["BuyDate"];
            customer.CarNum = (string)row["CarNum"];
            customer.BracketNum = (string)row["BracketNum"];
            customer.Brand = (string)row["Brand"];
            customer.TypeNum = (string)row["TypeNum"];
            customer.Suggestion = (string)row["Suggestion"];
            customer.Remarks = (string)row["Remarks"];
            return customer;
        }

        public int AddNew(CustomersModel model)
        {
            return SqlHelper.ExecuteNonQuery(@"insert into T_Customers(
                            Name,NamePinyin,TelPhone,MobilePhone,PostCode,Email,Address,DeptNo,BuyDate,CarNum,BracketNum,Brand,TypeNum,Suggestion,Remarks)
                            values (
                            @Name,@NamePinyin,@TelPhone,@MobilePhone,@PostCode,@Email,@Address,@DeptNo,@BuyDate,@CarNum,@BracketNum,@Brand,@TypeNum,@Suggestion,@Remarks)",
            new SqlParameter("@Name", model.Name),
            new SqlParameter("@NamePinyin", model.NamePinyin),
            new SqlParameter("@TelPhone", model.TelPhone),
            new SqlParameter("@MobilePhone", model.MobilePhone),
            new SqlParameter("@PostCode", model.PostCode),
            new SqlParameter("@Email", model.Email),
            new SqlParameter("@Address", model.Address),
            new SqlParameter("@DeptNo", model.DeptNo),
            new SqlParameter("@BuyDate", model.BuyDate),
            new SqlParameter("@CarNum", model.CarNum),
            new SqlParameter("@BracketNum", model.BracketNum),
            new SqlParameter("@Brand", model.Brand),
            new SqlParameter("@TypeNum", model.TypeNum),
            new SqlParameter("@Suggestion", model.Suggestion),
            new SqlParameter("@Remarks", model.Remarks));
        }


        public CustomersModel[] GetAll()
        {
            DataTable table = SqlHelper.ExecuteDataTable("select * from T_Customers");
            if (table.Rows.Count <= 0)
            {
                return null;
            }
            else
            {
                CustomersModel[] customers = new CustomersModel[table.Rows.Count];
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    CustomersModel model = ToCustomer(table.Rows[i]);
                    customers[i] = model;                       
                }
                return customers;
            }
        }

       

    }
}
